package mxm.amfire;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import me.yokeyword.fragmentation.SupportFragment;
import mxm.amfire.event.StartFragEvent;

/**
 * Created by zengpeijin on 2017/2/9.
 */

public class LogosFragment extends SupportFragment{
    RecyclerView recyclerView;
    private Integer res[]={R.drawable.flag_china,R.drawable.flag_germany,R.drawable.flag_italy,R.drawable.flag_usa,R.drawable.flag_japan,R.drawable.flag_england,R.drawable.flag_france,R.drawable.flag_other};
    private String titles[]={"中国","德国","意大利","美国","日本","英国","法国","其他"};
    private View rootView;


    public static LogosFragment newInstance() {
        Bundle args = new Bundle();
        LogosFragment fragment = new LogosFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_logos,container,false);
        recyclerView= (RecyclerView) rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(_mActivity,3));
        recyclerView.setAdapter(new RecyclerView.Adapter<MyViewHolder>() {
            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                MyViewHolder myViewHolder =new MyViewHolder(LayoutInflater.from(_mActivity).inflate(R.layout.recycer_item_main,null,false));
                return myViewHolder;
            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                holder.iv.setImageResource(res[position]);
                holder.tv.setText(titles[position]);
                holder.itemView.setOnClickListener(new MyOnClickListener(position));
            }

            @Override
            public int getItemCount() {
                return res.length;
            }

        });
    return rootView;
    }
    class MyViewHolder extends RecyclerView.ViewHolder
    {
        ImageView iv;
        TextView tv;
        public MyViewHolder(View view)
        {
            super(view);
            iv= (ImageView) view.findViewById(R.id.imageView);
            tv= (TextView) view.findViewById(R.id.textView);
        }
    }
    class MyOnClickListener implements View.OnClickListener{
        private int position;
        public  MyOnClickListener(int position){
            this.position=position;
        }
        @Override
        public void onClick(View view) {
            EventBus.getDefault().post(new StartFragEvent(LogoFragment.newInstance(position)));
        }
    }

}
